package com.ws.shop.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.ws.shop.bean.Page;
import com.ws.shop.dao.GoodsDao;
import com.ws.shop.entity.Accessory;
import com.ws.shop.entity.Goods;
import com.ws.shop.service.GoodsService;

@Service(value="goodsService")
public class GoodsServiceImpl implements GoodsService {
	@Resource(name="goodsDao")
	private GoodsDao goodsDao;

	@Cacheable(value="goodsColl")
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public Page findGoodsByPage(int pageno, int pagesize, String goodsname) {
		return goodsDao.findGoodsByPage(pageno,pagesize,goodsname);
	}

	@Caching(evict={
			@CacheEvict(value="goods",key="'findById_'+#goods.id",condition="#goods.id > 0"),
			@CacheEvict(value="goodsColl",allEntries=true)})
	@Transactional(rollbackFor=Exception.class)
	public int saveOrUpdate(Goods goods) {
		return goodsDao.saveOrUpdate(goods);
	}

	@CacheEvict(value="goods",key="'findAccessoryListByGoodsId_'+#id")
	@Transactional(rollbackFor=Exception.class)
	public void saveGoodsAccessory(int id, int[] imgs) {
		goodsDao.saveGoodsAccessory(id,imgs);
	}

	@Caching(evict={
			@CacheEvict(value="goods",key="'findById_'+#goods.id"),
			@CacheEvict(value="goodsColl",allEntries=true)})
	@Transactional(rollbackFor=Exception.class)
	public void deleteById(int id) {
		goodsDao.deleteById(id);
	}

	@Cacheable(value="goods",key="'findById_'+#id")
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public Goods findGoodsById(int id) {
		return goodsDao.findGoodsById(id);
	}

	@Cacheable(value="goods",key="'findAccessoryListByGoodsId_'+#goodsId")
	@Transactional(rollbackFor=Exception.class)
	public List<Accessory> findAccessoryListByGoodsId(int goodsId) {
		return goodsDao.findAccessoryListByGoodsId(goodsId);
	}

}
